<template>
  <el-dialog :visible="settingsVisible" :title="myIndex > -1? '编辑'+titleName : '新增'+titleName" @close="closeDialog(settingMode)">
    <!-- 商品分类 -->
    <el-form v-if="settingMode === 'goodsType'" ref="goodsType" :rules="rule" :model="settingForm">
      <el-form-item :label-width="labeWidth" label="分类名称" prop="name">
        <el-col :span="18" >
          <el-input v-model="settingForm.name" maxlength="20" placeholder="请填写分类名称"/>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="顺序" prop="sortOrder">
        <el-col :span="18" >
          <el-input v-model="settingForm.sortOrder" :disabled="tinymceDis" maxlength="2" type="text" placeholder="请输入顺序"/>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="是否启用" prop="usableStatus">
        <el-col :span="18" >
          <el-select v-model="settingForm.usableStatus" :disabled="tinymceDis" placeholder="请选择">
            <el-option :value="0" label="停用" />
            <el-option :value="1" label="启用" />
          </el-select>
        </el-col>
      </el-form-item>
      <div class="bottomBtn">
        <el-button type="primary" @click="submitForm('goodsType')">确定</el-button>
        <el-button type="info" @click="closeDialog(settingMode)">取消</el-button>
      </div>
    </el-form>

    <el-form v-if="settingMode === 'refundReason'" ref="refundReason" :rules="rule" :model="settingForm">
      <el-form-item :label-width="labeWidth" label="退货原因" prop="reason">
        <el-col :span="18" >
          <el-input v-model="settingForm.reason" maxlength="20" placeholder="请填写退货原因"/>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="顺序" prop="sort">
        <el-col :span="18" >
          <el-input v-model="settingForm.sort" :disabled="tinymceDis" maxlength="2" type="text" placeholder="请输入顺序"/>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="是否启用" prop="status">
        <el-col :span="18" >
          <el-select v-model="settingForm.status" :disabled="tinymceDis" placeholder="请选择">
            <el-option :value="0" label="停用" />
            <el-option :value="1" label="启用" />
          </el-select>
        </el-col>
      </el-form-item>
      <div class="bottomBtn">
        <el-button type="primary" @click="submitForm('refundReason')">确定</el-button>
        <el-button type="info" @click="closeDialog(settingMode)">取消</el-button>
      </div>
    </el-form>
    <!-- 分销返点 -->
    <!-- <el-form v-if="settingMode === 'rebate'" ref="rebate" :rules="rule" :model="settingForm">
      <el-form-item :label-width="labeWidth" label="返点" prop="name">
        <el-col :span="18" >
          <el-input v-model="settingForm.name" maxlength="20" placeholder="请填写返点数字"/>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="是否启用" prop="usableStatus">
        <el-col :span="18" >
          <el-select v-model="settingForm.usableStatus" :disabled="tinymceDis" placeholder="请选择">
            <el-option :value="0" label="停用" />
            <el-option :value="1" label="启用" />
          </el-select>
        </el-col>
      </el-form-item>
      <div class="bottomBtn">
        <el-button type="primary" @click="submitForm('rebate')">确定</el-button>
        <el-button type="info" @click="closeDialog(settingMode)">取消</el-button>
      </div>
    </el-form> -->
    <!-- 会员折扣 -->
    <!-- <el-form v-if="settingMode === 'discount'" ref="discount" :rules="rule" :model="settingForm">
      <el-form-item :label-width="labeWidth" label="会员类型" prop="sortOrder">
        <el-col :span="18" >
          <el-select v-model="settingForm.sortOrder" placeholder="请选择会员类型">
            <el-option
              v-for="(item,index) in momList"
              :key="index"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="折扣" prop="discount">
        <el-col :span="8" >
        <el-input v-model="settingForm.discount" type="text" placeholder="请输入数字字符"/></el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="是否启用" prop="usableStatus">
        <el-col :span="18" >
          <el-select v-model="settingForm.usableStatus" :disabled="tinymceDis" placeholder="请选择">
            <el-option :value="0" label="停用" />
            <el-option :value="1" label="启用" />
          </el-select>
        </el-col>
      </el-form-item>
      <div class="bottomBtn">
        <el-button type="primary" @click="submitForm('discount')">确定</el-button>
        <el-button type="info" @click="closeDialog(settingMode)">取消</el-button>
      </div>
    </el-form> -->

    <!-- 会员复利返点 -->
    <!-- <el-form v-if="settingMode === 'membersRebate'" ref="membersRebate" :rules="rule" :model="settingForm">
      <el-form-item :label-width="labeWidth" label="会员类型" prop="sortOrder">
        <el-col :span="18" >
          <el-select v-model="settingForm.sortOrder" placeholder="请选择会员类型">
            <el-option
              v-for="(item,index) in momList"
              :key="index"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="返点" prop="discount">
        <el-col :span="8" >
        <el-input v-model="settingForm.discount" type="text" placeholder="请输入数字字符"/></el-col>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="是否启用" prop="usableStatus">
        <el-col :span="18" >
          <el-select v-model="settingForm.usableStatus" :disabled="tinymceDis" placeholder="请选择">
            <el-option :value="0" label="停用" />
            <el-option :value="1" label="启用" />
          </el-select>
        </el-col>
      </el-form-item>
      <div class="bottomBtn">
        <el-button type="primary" @click="submitForm('membersRebate')">确定</el-button>
        <el-button type="info" @click="closeDialog(settingMode)">取消</el-button>
      </div>
    </el-form> -->
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { vipInfoAll } from '@/api/product'
// <sendGoods :send-goods-visible.sync="sendGoods" :send-goods-index="sendGoodsIndex" :send-goods-id="sendGoodsId"/>
export default {
  props: {
    settingsVisible: {
      type: Boolean,
      required: true
    },
    myIndex: {
      type: Number,
      required: true
    },
    dialogData: {
      type: Object,
      required: true
    },
    settingMode: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labeWidth: '120px',
      goodTypesForm: {},
      settingForm: {},
      tinymceDis: false,
      discountList: [],
      momList: [],
      rule: {
        name: [
          { required: true, message: '请填写分类名称', trigger: 'blur' }
        ],
        sortOrder: [
          { required: true, message: '请输入顺序', trigger: 'blur' },
          { pattern: /^[0-9]+\d*$/, message: '请输入正整数', trigger: 'blur' }
        ],
        usableStatus: [
          { required: true, message: '请选择是否启用', trigger: 'blur' }
        ],
        discount: [
          { required: true, message: '请填写会员折扣', trigger: 'blur' },
          { pattern: /^(\d+)(.\d{0,2})?$/, message: '请输入正确的数字，并且小数点后最多保留两位数字', trigger: 'blur' }
        ],
        // 退货原因校验 👇👇👇
        reason: [
          { required: true, message: '请填写退货原因', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入顺序', trigger: 'blur' },
          { pattern: /^[0-9]+\d*$/, message: '请输入正整数', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择是否启用', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    titleName() {
      const titleMap = {
        'goodsType': '商品分类',
        'rebate': '分销返点',
        'discount': '会员折扣',
        'membersRebate': '会员复利返点',
        'refundReason': '退货原因'
      }
      return titleMap[this.settingMode]
    }
  },
  watch: {
    myIndex(val) {
      if (val > -1) {
        this.settingForm = Object.assign({}, this.dialogData)
      } else {
        this.settingForm = {}
      }
    }
  },
  mounted() {
    // if (this.settingMode === 'discount' || this.settingMode === 'membersRebate') {
    //   // this.getConstantPercent()
    //   this.getMomList()
    // }
  },
  methods: {
    getMomList() {
      vipInfoAll().then(res => {
        if (res.code === '200') {
          this.momList = res.data
        } else {
          Message.error(res.message)
        }
      })
    },
    submitForm(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          // 向父级插件冒一个‘updataParent’ 的方法, 并且把填好的数据带过去， 到父级去做处理
          this.$emit('updataParent', this.settingForm)
          this.$emit('update:myIndex', 0)
          this.closeDialog(this.settingMode)
        }
      })
    },
    closeDialog(settingMode) {
      this.$emit('update:settingsVisible', false)
      this.$refs[settingMode].clearValidate()
    }
  }
}
</script>
<style scope>
.bottomBtn{
    padding-top: 20px;
    width:100%;
    text-align: center;
    border-top: 1px solid #ddd
}
.bottomBtn button{
    display: inline-block;
}
select{
    width:100%
}
</style>
